package com.tlgen.rpc.config;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

public class FetchFactoryBean<T> implements FactoryBean<T> {

    private Class<T> fetchInterface;

    public void setFetchInterface(Class<T> fetchInterface) {
        this.fetchInterface = fetchInterface;
    }

    public FetchFactoryBean() {
        this.fetchInterface = fetchInterface;
    }

    @Override
    public T getObject() throws Exception {
        return (T) Proxy.newProxyInstance(fetchInterface.getClassLoader(),
                new Class[]{fetchInterface}, new FetchProxy<T>(fetchInterface));
    }

    @Override
    public Class<?> getObjectType() {
        return this.fetchInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

}
